const time = require('../../utils/time.js')
//获取应用实例
const app = getApp()
const db = wx.cloud.database()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    times: [
      "08:30-10:00",
      "10:15-11:45",
      "14:30-16:00",
      "16:15-17:45",
      "19:00-20:30",
    ],
    is_login: false,
    is_bind: false,
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 800,
    circular: true,
    user_data: {},
    show: false, // 授权弹窗的显示
    // 轮播图
    imgUrls: [
      {
        url: "cloud://cloud1-8getd32w62af1055.636c-cloud1-8getd32w62af1055-1308569496/match/05.jpg",
        name: "丁颖杯"
      },
      {
        url: "cloud://cloud1-8getd32w62af1055.636c-cloud1-8getd32w62af1055-1308569496/match/11.jpg",
        name: "蓝桥杯"
      },
      {
        url: "cloud://cloud1-8getd32w62af1055.636c-cloud1-8getd32w62af1055-1308569496/match/16.jpg",
        name: "数据应用创新大赛"
      },
      {
        url: "cloud://cloud1-8getd32w62af1055.636c-cloud1-8getd32w62af1055-1308569496/match/17.jpg",
        name: "百度网络安全大赛"
      },

    ],

    // 主要功能
    main: [{
      key: "collegeIntroduce",
      desc: "院系介绍",
    },
    {
      key: "viewIntroduce",
      desc: "美景介绍",
    },
    {
      key: "canteenIntroduce",
      desc: "美食介绍",
    },
    {
      key: "matchIntroduce",
      desc: "比赛介绍",
    }
    ],

    // 更多功能
    navs: [{
      key: "timetable",
      desc: "课表",
    },
    {
      key: "score",
      desc: "成绩",
    },
    {
      key: "lost",
      desc: "失物招领",
    },
    {
      key: "lovewall",
      desc: "表白墙",
    },
    {
      key: "more",
      desc: "更多功能",
    }
    ],


  },

  //轮播图点击事件
  swipclick: function (e) {
    console.log(e.currentTarget.dataset);
    wx.navigateTo({
      url: '../detail/matchDetail/matchDetail' + '?match=' + e.currentTarget.dataset.item.name,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getData()
    //this.getTodayClass()

  },

  //获取用户登录、课表等数据
  getData() {

    console.log('是否已授权');
    console.log(wx.getStorageSync('is_login'));
    console.log('是否已绑定教务系统');
    console.log(wx.getStorageSync('is_bind'));
    this.setData({
      is_login: wx.getStorageSync('is_login'),
      is_bind: wx.getStorageSync('is_bind')
    })
    app.globalData.is_login = wx.getStorageSync('is_login');
    app.globalData.is_bind = wx.getStorageSync('is_bind');

    wx.setStorageSync('user', app.globalData.user_data);
    console.log(time.formatTime_year_month_day(new Date()));

  },

  // 玩玩学校登录验证的接口
  test() {
    wx.request({
      url: 'https://cas.scau.edu.cn/lyuapServer/login?service=https%3A%2F%2Fportal.scau.edu.cn%2Fc%2Fportal%2Flogin%3Fredirect%3D%252F%26p_l_id%3D1144776', //仅为示例，并非真实的接口地址
      data: {
        username: "202025510224",
        password: "82d0b1c68c883ef29243eca78c05e12a8e24b9ffa0a96fac103b5063f63877380876af497d32b4761c30db335d6b090c8002011e2f9234a29a78fc96222f1d07d7beeb7504fe42f9f35354c5a7cade87bca6d0ec9ad17faf2cccdd9d431792ecf36e82049543f724d74f9940ebcf612cbe8182f32630637af70481fbc70ca9ee",
        captcha: "",
        warn: "true",
        It: "LT-105973-PFLxxPnHBJjFzfyvrotlO5ufX1vmG4-cas01.example.org",
        execution: "e1s2",
        _eventId: "submit",
        submit: "登 录"
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      method: "POST",
      success(res) {
        console.log(res.data)
      },
      fail(err) {
        console.log(err)
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // if (app.user_token) {
    //   this.data.pull = true
    //   this.getStuclass()
    // } else
    //   wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /*跳转到教务系统登录*/
  auth: function (e) {
    // console.log(e)
    wx.navigateTo({
      url: '/pages/login/login'
    })
  },

  // 打开授权弹窗
  add_share() {

    let shouquan = app.globalData.is_login;
    // console.log(shouquan);
    if (!shouquan) {
      // 如果还没授权，那就授权
      this.setData({
        show: true
      });
    }

  },


  hideModel() {
    this.setData({
      show: false
    })
  },

  submit: function (e) {

    console.log(e.detail.target.dataset.key);

    var key = e.detail.target.dataset.key //要去的地方。
    var content = ""

    // 是否绑定了教务网，绑定了才可以通过
    let is_bind = wx.getStorageSync('is_bind')

    if (!is_bind) {
      wx.showModal({
        title: '绑定提示',
        content: content,
        confirmText: "去绑定",
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/login/login',
            })
          }
        }
      })
      return;
    } else {

      wx.navigateTo({
        url: '/pages/core/' + key + "/" + key,
        fail: function () {
          wx.showToast({
            title: '即将开放',
            icon: 'none'
          })
        }
      })


    }
  },


  // 授权，读取用户的头像昵称，同时检查是不是新用户
  getUserProfile(e) {
    const that = this;
    // 先登录，获取到openid，然后存起来
    // 然后下面进行获取用户数据
    wx.getUserProfile({
      desc: '为了更好的体验',
      success: res => {
        console.log(res);
        const { avatarUrl, nickName, gender } = res.userInfo;

        db.collection('users').where({
          _openid: wx.getStorageSync('openid')
        }).update({
          // data 传入需要局部更新的数据
          data: {
            avatarUrl,
            nickName,
            gender
          },
          success: function (res) {
            console.log(res);
            that.getData();
          },
          fail: function (err) {
            console.log(err);
          }
        })

        that.init(res.userInfo);

      }
    })

  },

  init(userInfo) {
    const {avatarUrl, nickName, gender } = userInfo;

    app.globalData.is_login = true; // 已经授权过用户头像和昵称了，is_login变为true
    wx.setStorageSync('is_login', true); // 设置本地缓存

    app.globalData.user_data.avatarUrl = avatarUrl;
    app.globalData.user_data.nickName = nickName;
    app.globalData.user_data.gender = gender;
    wx.setStorageSync('user', app.globalData.user_data); // 更新一下本地缓存

    wx.showToast({
      title: '授权成功',
      icon: 'success'
    });

    this.setData({
      show: false,
      is_login: true
    })

  }



})